//
//  checkModifyPwd.m
//  DaoZiJinFu
//
//  Created by woker on 16/7/20.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "checkModifyPwd.h"

@implementation checkModifyPwd

+ (instancetype)showView:(UIView *)view withMoney:(NSString *)money {
    checkModifyPwd *modifyPwd = [[[NSBundle mainBundle] loadNibNamed:@"checkModifyPwd" owner:nil options:nil] lastObject];
    modifyPwd.withdrawalsLab.text = [NSString stringWithFormat:@"提现%.2lf元",[money doubleValue]];
    [view addSubview:modifyPwd];
    
    modifyPwd.frame = SCREEN_FRAME;
    
//    [modifyPwd mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(view.mas_top);
//        make.leading.equalTo(view.mas_leading);
//        make.bottom.equalTo(view.mas_bottom);
//        make.trailing.equalTo(view.mas_trailing);
//    }];
    
    [modifyPwd readjustView];
    
    return modifyPwd;
}

- (IBAction)closeClick:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)forgetModifyPwdClick:(id)sender {
    [self.delegate resetModifyPwd];
}

- (void)readjustView {
    _modifyPwdView.layer.borderWidth = 1;
    _modifyPwdView.layer.borderColor = BACKGROUND_COLOR.CGColor;
    _dealPwd = [NSMutableString string];
    
    _modifyText.delegate = self;
    
    [_modifyText becomeFirstResponder];
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length == 6 && text.length) {
        return NO;
    }
    
    if ([CommTools isPureNumandCharacters:text] || text.length == 0) {
        NSInteger preIndex = 0;
        NSInteger nowIndex = 0;
        
        preIndex = textView.text.length;
        if (text.length) {
            NSString *textStr =  [textView.text stringByAppendingString:text];
            _dealPwd = [NSMutableString stringWithString:textStr];
        } else {
            if (_dealPwd.length > 0) {
                NSString *updateStr = [_dealPwd substringWithRange:NSMakeRange(0, _dealPwd.length-1)];
                _dealPwd = [NSMutableString stringWithString:updateStr];
            }
        }
//        NSLog(@"dealPwd = %@",_dealPwd);
        
        nowIndex = _dealPwd.length;
        
        if (preIndex < nowIndex) {
            switch (preIndex) {
                case 0: _img1.hidden = NO; break;
                case 1: _img2.hidden = NO; break;
                case 2: _img3.hidden = NO; break;
                case 3: _img4.hidden = NO; break;
                case 4: _img5.hidden = NO; break;
                case 5: _img6.hidden = NO; break;
            }
        } else if (preIndex > nowIndex) {
            switch (nowIndex) {
                case 0: _img1.hidden = YES; break;
                case 1: _img2.hidden = YES; break;
                case 2: _img3.hidden = YES; break;
                case 3: _img4.hidden = YES; break;
                case 4: _img5.hidden = YES; break;
                case 5: _img6.hidden = YES; break;
            }
        }
        
        
        if (_dealPwd.length == 6) {
            [self.delegate doWithModifypwd:_dealPwd];
        }
        return YES;
    }
    return NO;
}

- (void)clearPwdText {
    _modifyText.text = @"";
    _img1.hidden = YES;
    _img2.hidden = YES;
    _img3.hidden = YES;
    _img4.hidden = YES;
    _img5.hidden = YES;
    _img6.hidden = YES;
}

@end
